package com.ljc.ssm.interceptor;

import com.alibaba.druid.util.StringUtils;
import com.ljc.ssm.common.BizException;
import com.ljc.ssm.common.ExceptionCode;
import com.ljc.ssm.common.BizException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 * User: ljc
 * Date: 2022/12/13 11:25
 * Description:
 * Version: V1.0
 *
 * preHandler:执行前先做的操作(主要)
 * postHandler:执行后
 * afterHandler:最终
 * @author: ljc
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {
    /**
     * true代表放行,false代表拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)){
            throw new BizException(ExceptionCode.NULL_TOKEN);
        }
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
